package com.cjie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjie.domain.ResponseResult;
import com.cjie.domain.entity.Article;
import com.cjie.domain.entity.Category;
import com.cjie.domain.vo.ArticleDetailVo;
import com.cjie.domain.vo.ArticleListVo;
import com.cjie.domain.vo.HotArticleVo;
import com.cjie.domain.vo.PageVo;
import com.cjie.mapper.ArticleMapper;
import com.cjie.service.ArticleService;
import com.cjie.service.CategoryService;
import com.cjie.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.cjie.constants.SystemConstants.ARTICLE_STATUS_NORMAL;
import static com.cjie.constants.SystemConstants.STATUS_NORMAL;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询热门文章列表
     *
     * @return
     */
    @Override
    public ResponseResult<Article> getHotArticleList() {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 过滤删除文章
        articleLambdaQueryWrapper.eq(Article::getDelFlag, ARTICLE_STATUS_NORMAL);
        // 过滤草稿文章
        articleLambdaQueryWrapper.eq(Article::getStatus, ARTICLE_STATUS_NORMAL);
        // 降序排列
        articleLambdaQueryWrapper.orderByDesc(Article::getViewCount);
        // 最多查询十条
        Page<Article> articlePage = new Page(1, 10);
        page(articlePage, articleLambdaQueryWrapper);
        List<Article> articleList = articlePage.getRecords();

        // 使用自定义对象拷贝工具类
        List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeanList(articleList, HotArticleVo.class);

        return ResponseResult.okResult(hotArticleVos);
    }

    /**
     * 分页查询博文列表
     *
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult getArticleList(Long categoryId, Integer pageNum, Integer pageSize) {
        // 查询条件
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据分类ID查询
        articleLambdaQueryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0, Article::getCategoryId, categoryId);
        // 发布状态为正式发布
        articleLambdaQueryWrapper.eq(Article::getStatus, ARTICLE_STATUS_NORMAL);
        // 对isTop进行降序排列
        articleLambdaQueryWrapper.orderByDesc(Article::getIsTop);
        // 分页查询
        Page<Article> articlePage1 = new Page<>(pageNum, pageSize);
        page(articlePage1, articleLambdaQueryWrapper);

        // 查询分类名称
        List<Article> articles = articlePage1.getRecords();
        articles.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());

        // 封装查询结果
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articlePage1.getRecords(), ArticleListVo.class);

        // 封装到pageVo
        PageVo pageVo = new PageVo(articleListVos, articlePage1.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    @Override
    public ResponseResult getArticleDetail(Long id) {
        // 根据id查询文章
        Article article = getById(id);
        // 转换成vo
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        // 根据分类id查询分类名
        Long articleDetailVoCategoryId = articleDetailVo.getCategoryId();
        Category category = categoryService.getById(articleDetailVoCategoryId);
        if (category != null) {
            articleDetailVo.setCategoryName(category.getName());
        }
        // 封装响应结果
        return ResponseResult.okResult(articleDetailVo);
    }


}
